<?php
class func {//公共方法类
	
	var $_is_html = 0;	//定义是否使用模板
	var $_url;//定义网站路径
	var $_mb = 'recruit';//定义网址模板
	var $_vtype = 'pc';//pc or phone or weixin
	var $style;//定义样式
	var $adstyle;//定义后台样式
	var $kfstyle;//定义客服样式
	var $_img;//定义图片格式
	var $_imgsize;//定义图片大小 默认1M
	var $_filesize;//定义图片大小 默认1M
	var $file_type;//定义附件格式
	var $gnpd;
	var $uid;
	
	function __construct(){
		$this->v_get = $this->_get();
		$this->v_get['met'] = @$this->_get('met') ? @$this->_get('met') : 'index';
		$this->_con = $con = C_base::M('admin.base')->con_s();//读取网站设置
		$this->_img = $con['imgtype'] ? $con['imgtype'] : 'jpg,jpeg,png,gif,bmp';
		$this->_imgsize = $con['imgsize'] ? $con['imgsize'] : '1';
		$this->_filesize = $con['filesize'] ? $con['filesize'] : '1';
		$this->file_type = explode(',',$con['filetype']);
		$this->_url = $con['url'];//网址
		//$this->_vtype = util::isMobile();//pc or phone or weixin
		$this->adstyle = $this->_url.'/admin/assets';//后台样式地址
		$this->kfstyle = $this->_url.'/kefu';//客服样式地址
		$this->style = $this->_url.'/style/'.$this->_mb.'/'.$this->_vtype;//html样式地址
		$gnid = @$this->_get('gnid') ? '&gnid='.$this->_get('gnid') : '';
		$dbs = @$this->_get('dbs') ? '&dbs='.$this->_get('dbs') : '';
		$ebs = @$this->_get('ebs') ? '&ebs='.$this->_get('ebs') : '';
		$xbs = @$this->_get('xbs') ? '&xbs='.$this->_get('xbs') : '';
		$this->gnpd = $gnid.$dbs.$ebs.$xbs;
		$this->uid = @self::Ses('id');//当前登录用户id
		
	}
	
/**************************************get,post，file处理**********************************/
	/**
	 * 封装$_GET方法
	 * 考虑 $_GET传过来的参数可能是变量，也可能是数组
	 * so 要对传过来的参数做判断
	 * type = 0 过滤 默认
	 * type = 1 不过滤
	 */
	function _get($a ='',$type = 0){
		if($type){
			//$a = $this->inject_check($a);
			//$da = @$a ? $a : $_GET;
			$da = @$_GET[$a] ? $_GET[$a] : $_GET;
		}else{
			if ($a){   //传过来的为一变量直接过滤
				//$da = $this->inject_check(self::guol($_GET[$a]));
				$da = self::guol($_GET[$a]);
			}else{	  //传过来的为数组，遍历过滤
				$da = array();
				foreach (@$_GET as $k=>$v){
					if($v){
						//$da[$k] = $this->inject_check(self::guol($v));
						$da[$k] = self::guol($v);
					}else{
						$da[$k] = $v;
					}
				}
			}
		}
		return $da;
	}
	
	function inject_check($sql_str) {
		$gl = '/select|insert|and|or|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile/is';  //一些标记
		$str = preg_replace($gl,'',$sql_str);
		return $str;
	}
	/**
	 * 封装$_POST方法
	 * 同$_GET
	 */
	function _post1($a ='',$type = 0){
		if($type){
			$a = $this->inject_check($a);
			$da = @$a ? $a : $_POST;
			//$da = @$_POST[$a] ? $_POST[$a] : $_POST;
		}else{
			if ($a){
				$da = $this->inject_check(self::guol($_POST[$a]));
				//$da =  self::guol($_POST[$a]);
			}else{
				$da = array();
				foreach (@$_POST as $k=>$v){
					if($v){
						$da[$k] = $this->inject_check(self::guol($v));
						//$da[$k] = self::guol($v);
					}else{
						$da[$k] = $v;
					}
				}
			}
		}
		return $da;
	}
	
	function _post($a ='',$type = 0){
		if($type){
			$da = @$_POST[$a] ? $_POST[$a] : $_POST;
		}else{
			if ($a){
				//$da = $this->inject_check(self::guol($_POST[$a]));
				$da =  self::guol($_POST[$a]);
			}else{
				$da = array();
				foreach (@$_POST as $k=>$v){
					if($v){
						//$da[$k] = $this->inject_check(self::guol($v));
						$da[$k] = self::guol($v);
					}else{
						$da[$k] = $v;
					}
				}
			}
		}
		return $da;
	}
	
	/**
	 * 获取上传文件信息
	 * name	文件名
	 */
	function _file($name = 'file'){
		return $_FILES[$name];
	}
	
	/**
	 * 过滤 post get 的参数，防止非法参数
	 */
	function guol($str){
		$gl = '/<(script|head|body|title|link|html|meta|\?|\%)[^>]*?>/is';  //一些标记
		$str = preg_replace($gl,'',$str);
		return $str;
	}

/**************************************cookie、session处理*********************************/
	/**
	 * cookie封装处理
	 * $k 键名
	 * $v 值
	 * cookie的有效期，天数，默认为1天
	 */
	static function cookie($k=null,$v=null,$day=1){
		if($v !== null){
			$_COOKIE[$k] = $v;
			$domain = $_SERVER['HTTP_HOST'];//域名
			if ($day < 1){//如果小于1，则清除cookie
				setcookie($k,'',time()-3600,'/',$domain,false);
			}else{//存储cookie
				$time = 3600 * 24 * $day;//$day天
				setcookie($k,$v,time()+$time,'/',$domain,false);
			}
		}elseif($k !== null){
			return $_COOKIE[$k];
		}else{
			return $_COOKIE;
		}
	}
	
	/**
	 * cookie全清空
	 */
	static function cookie_del(){
		foreach($_COOKIE as $k=>$v){
			self::cookie($k,'',-1);
		}
	}
	
	/**
	 * session封装
	 */
	static function Ses($k = '',$v = null){
		if($v !== null){
			$_SESSION[$k] = $v;
		}elseif ($k){
			$kk = explode('.',$k);
			if (count($kk) > 1){
				$sess = $_SESSION[$kk[0]];
				for ($i=1;$i<count($kk);$i++){
					$sess = $sess[$kk[$i]];
				}
				return  $sess;
			}else{
				return $_SESSION[$k];
			}
		}
		return session_id();
	}
/**************************************弹窗处理*********************************************/
	/**
	 * $k 弹出信息
	 * $v 跳转路径
	 * js页面跳转
	 */
	function _alert_location($k,$v = ''){
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
		if ($v) {
			exit('<script language="javascript">alert("'.$v.'");top.location.href="'.$k.'";</script>');
		}else {
			exit('<script language="javascript">window.location.href="'.$k.'";</script>');
		}
	}
	
	//错误跳转
	function _cuowu($vn){
		echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
		exit("<script language='javascript'>alert('".$vn."');history.go(-1);</script>");
	}

}